import { convertDateSelectValue } from 'components/dateSelect'

export default function (element, fields) {
	const result = {}
	if (
		element.data.formType == 'product' ||
		element.data.formType == 'program' ||
		element.data.formType == 'product-solution'
	) {
		getProductParams(result, element)
	} else if (element.data.formType == 'wangEditor') {
		result[element.key] = {
			html: element.value.html
		}
	} else if (
		element.data.formType == 'group' ||
		element.data.formType == 'unit' ||
		element.data.formType == 'selectinput'
	) {
		result[element.key] = element.value
	} else if (element.data.formType == 'dateselect') {
		result[element.key] = convertDateSelectValue(element.value)
	} else if (element.data.formType == 'inputrange') {
		result[element.key] = element.value
	} else if (
		element.data.formType == 'supplier' &&
		element.data.fieldType == 2
	) {
		result[element.key] = element.value
	} else if (element.data.formType == 'channel') {
		result[element.key] = element.value
	} else if (element.data.formType == 'stageplans') {
		result[element.key] = element.value
	} else if (element.data.formType == 'datatable') {
		result[element.key] = element.value
	} else if (element.data.formType == 'multSelect') {
		result[element.key] = element.value
	} else if (element.data.formType == 'multTreeSelect') {
		result[element.key] = element.value.join(',')
	} else if (
		[
			'businesspersonal',
			'contractpersonal',
			'customerpersonal',
			'relation',
			'contacts',
			'customer',
			'contract',
			'business',
			'receivables_plan',
			'suppliercontacts',
			'payable',
			'supplier',
			'person',
			'claim'
		].indexOf(element.data.formType) > -1
	) {
		const key = element.data.primaryKey || element.key // primaryKey 备选relative行中的主键
		if (element.value.length) {
			result[element.key] = element.value.map((v) => v[key]).join(',')
		} else {
			result[element.key] = ''
		}
	} else if (element.data.formType == 'category') {
		result[element.key] = JSON.stringify(element.value)
	}

	return result
}

function getProductParams(params, element) {
	if (element.value) {
		params['product'] = element.value.product ? element.value.product : []
		params['totalPrice'] = element.value.totalPrice
			? element.value.totalPrice
			: 0
		params['discountRate'] = element.value.discountRate
			? element.value.discountRate
			: 0
	} else {
		params['product'] = []
		params['totalPrice'] = ''
		params['discountRate'] = ''
	}
}
